package gateway.route;

import cn.hutool.setting.yaml.YamlUtil;
import org.springframework.cloud.gateway.filter.FilterDefinition;
import org.springframework.cloud.gateway.handler.predicate.PredicateDefinition;
import org.springframework.cloud.gateway.route.RouteDefinition;
import org.springframework.cloud.gateway.route.RouteDefinitionRepository;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.*;

//@Repository
public class OldDatabaseRouteDefinitionRepository implements RouteDefinitionRepository {

    @Override
    public Flux<RouteDefinition> getRouteDefinitions() {

        List<PredicateDefinition> predicates = null;
        try {
            predicates = getPredicates("/Users/mac/IdeaProjects/SpringCloudGateWay/src/main/resources/application.yml");
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
        List<RouteDefinition> routeDefinitions = new ArrayList<>();
//        for (PredicateDefinition definition : predicates) {
//            RouteDefinition routeDefinition = new RouteDefinition();
//            routeDefinition.setId(rule.getRouteId());
//            routeDefinition.setUri(URI.create(rule.getUri()));
//            routeDefinition.setPredicates(this.getPredicates(rule.getPredicates()));
//            routeDefinition.setFilters(this.getFilters(rule.getFilters()));
//            routeDefinition.setMetadata(this.getMetadata(rule.getMetadata()));
//            Integer ruleOrder = rule.getRouteOrder();
//            if (null != ruleOrder) {
//                routeDefinition.setOrder(ruleOrder);
//            }
//            routeDefinitions.add(routeDefinition);
//        }
        return Flux.fromIterable(routeDefinitions);
    }



    public List<PredicateDefinition> getPredicates(String text) throws FileNotFoundException {
        if (StringUtils.isEmpty(text)) {
            return Collections.EMPTY_LIST;
        }

        LinkedHashSet<String> predicateList = YamlUtil.load(new FileReader(text), LinkedHashSet.class);
        List<PredicateDefinition> predicateDefinitions = new ArrayList<>();
        for (String predicate : predicateList) {
            if (StringUtils.isEmpty(predicate)) {
                continue;
            }
            PredicateDefinition definition = new PredicateDefinition(predicate);
            predicateDefinitions.add(definition);
        }
        return predicateDefinitions;
    }

    private List<FilterDefinition> getFilters(String text) throws FileNotFoundException {
        if (StringUtils.isEmpty(text)) {
            return Collections.EMPTY_LIST;
        }
        List<String> filterList = YamlUtil.load(new FileReader(text), List.class);
        List<FilterDefinition> filterDefinitions = new ArrayList<>();
        for (String filter : filterList) {
            if (StringUtils.isEmpty(filter)) {
                continue;
            }
            FilterDefinition definition = new FilterDefinition(filter);
            filterDefinitions.add(definition);
        }
        return filterDefinitions;
    }

    private Map<String, Object> getMetadata(String text) throws FileNotFoundException {
        if (StringUtils.isEmpty(text)) {
            return Collections.EMPTY_MAP;
        }
        return YamlUtil.load(new FileReader(text), Map.class);
    }

    @Override
    public Mono<Void> save(Mono<RouteDefinition> route) {
        return null;
    }

    @Override
    public Mono<Void> delete(Mono<String> routeId) {
        return null;
    }


}

